package com.wang.t;
import com.wang.t.entity.*;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;
import com.wang.t.dao.UserDao;
import com.wang.t.entity.User;
import com.wang.t.service.UserService;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	@Autowired
	private UserService userService;
////	public HomeController(){
//protected ModelAndView handle(HttpServletRequest req,
//			HttpServletResponse res, Object command, BindException e)
//			throws Exception {
//		LoginCommand loginCommand = (LoginCommand)command;
//		System.out.print("username="+loginCommand.getUsername());
//		if(userService.checkUser(loginCommand.getUsername(),loginCommand.getPassword())){
//			
//			User user = userService.findUserByUserName(loginCommand.getUsername());
//			user.setLastIP(req.getLocalAddr());
//			user.setLastVisit(new Date());
//			userService.loginSuccess(user);
//			req.getSession().setAttribute("user", user);
//			return new ModelAndView("main");
//		}else{
//			//用户不存在�û�������
//			return new ModelAndView("login","error","用户名或密码错误");
//		}
//		
//	}
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	
	@RequestMapping(value = "/home",params = "method=add2")  
    public String login2(User user,Model model) {  
		//model.addAttribute("user",user);  		
		System.out.println("aaa: "+user);
		model.addAttribute("user",user);  	
		if ("spring".equals(user.getUsername())	&& "spring".equals(user.getPassword())) {
			int matchCount = 123;
			matchCount = userService.checkUser();
		    model.addAttribute("serverTime", matchCount);
			return "home";
		}
		
		model.addAttribute("msg","asdasd");  	
		return  "main";
    }   
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/home1") 
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! the client locale is "+ locale.toString());
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		return "main";
	}
	
	@RequestMapping(value = "/")  
	public String HomeController(Model model){  
	    model.addAttribute("nidaye", "大爷21你好");  
	    
	    ArrayList<city> addresses = new ArrayList<city>();
	    ArrayList<city> addresses2 = new ArrayList<city>();
	    city myct = new city();
	    myct.setCityCD(1);
	    myct.setCityNM("北京");
	    addresses.add(myct);
	    addresses2.add(myct);
	    
	    myct = new city();
	    myct.setCityCD(2);
	    myct.setCityNM("上海");
	    addresses.add(myct);	    
	    addresses2.add(myct);
	    
	    myct = new city();
	    myct.setCityCD(3);
	    myct.setCityNM("长沙");
	    addresses.add(myct);
	    
	    myct = new city();
	    myct.setCityCD(4);
	    myct.setCityNM("济南");
	    addresses.add(myct);
	    
	    
	    
	    
	    
	    
	    Map<String, String> cityMap = new LinkedHashMap<String, String>();
	    
	    cityMap.put("1", "北京");
	    cityMap.put("2", "上海");
	    cityMap.put("3", "天津");
	    cityMap.put("4", "济南");
	    model.addAttribute("cityMap",cityMap);
	    
	    model.addAttribute("addresses",addresses);
	    model.addAttribute("addresses2",addresses2);

	    System.out.println("login.jsp");
	    return "login";
	}  
	
	@RequestMapping(value = "/getcd/{id}")  
	public String HomeController2(@PathVariable Integer id,Model model){  
	    model.addAttribute("nidaye", "大爷21你好");  
	    
	    ArrayList<city> addresses = new ArrayList<city>();
	    ArrayList<city> addresses2 = new ArrayList<city>();
	    city myct = new city();
	    myct.setCityCD(1);
	    myct.setCityNM("北京");
	    addresses.add(myct);
	    if(id != 2)
	    {
	    	addresses2.add(myct);
	    }
	    
	    myct = new city();
	    myct.setCityCD(2);
	    myct.setCityNM("上海");
	    addresses.add(myct);
	    if(id != 2)
	    {
	    	addresses2.add(myct);
	    }
	    
	    myct = new city();
	    myct.setCityCD(3);
	    myct.setCityNM("长沙");
	    addresses.add(myct);
	    if(id == 2)
	    {
	    	addresses2.add(myct);
	    }
	    
	    myct = new city();
	    myct.setCityCD(4);
	    myct.setCityNM("济南");
	    addresses.add(myct);
	    if(id == 2)
	    {
	    	addresses2.add(myct);
	    }
	    
	    
	    
	    
	    
	    Map<String, String> cityMap = new LinkedHashMap<String, String>();
	    
	    cityMap.put("1", "北京");
	    cityMap.put("2", "上海");
	    cityMap.put("3", "天津");
	    cityMap.put("4", "济南");
	    model.addAttribute("cityMap",cityMap);
	    
	    model.addAttribute("addresses",addresses);

	    System.out.println("login.jsp");
	    return "login";
	}  
	
	@RequestMapping(value = "/getcd")  
	public String HomeController4(Model model){  
	    model.addAttribute("nidaye", "大爷21你好");  
	    int id = 0;
	    ArrayList<city> addresses = new ArrayList<city>();
	    ArrayList<city> addresses2 = new ArrayList<city>();
	    city myct = new city();
	    myct.setCityCD(1);
	    myct.setCityNM("北京");
	    addresses.add(myct);
	    if(id != 2)
	    {
	    	addresses2.add(myct);
	    }
	    
	    myct = new city();
	    myct.setCityCD(2);
	    myct.setCityNM("上海");
	    addresses.add(myct);
	    if(id != 2)
	    {
	    	addresses2.add(myct);
	    }
	    
	    myct = new city();
	    myct.setCityCD(3);
	    myct.setCityNM("长沙");
	    addresses.add(myct);
	    if(id == 2)
	    {
	    	addresses2.add(myct);
	    }
	    
	    myct = new city();
	    myct.setCityCD(4);
	    myct.setCityNM("济南");
	    addresses.add(myct);
	    if(id == 2)
	    {
	    	addresses2.add(myct);
	    }
	    
	    
	    
	    
	    
	    Map<String, String> cityMap = new LinkedHashMap<String, String>();
	    
	    cityMap.put("1", "北京");
	    cityMap.put("2", "上海");
	    cityMap.put("3", "天津");
	    cityMap.put("4", "济南");
	    model.addAttribute("cityMap",cityMap);
	    
	    model.addAttribute("addresses",addresses);

	    System.out.println("login.jsp");
	    return "login";
	}  
	
	@RequestMapping(value = "/login")  
	public String HomeController3(Model model){  
	    model.addAttribute("nidaye", "大爷21你好");  
	    System.out.println("login.jsp");
	    return "login";
	   
	}  
	
	private UserDao userDao;
	@RequestMapping(value = "/home")  
	public void HomeController1(Model model){  
		int matchCount = 123;
		matchCount = userService.checkUser();
	    model.addAttribute("serverTime", matchCount);  
	    System.out.println("login.jsp");
	   
	} 
	
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView createUser(User user) {
		//userService.createUser(user);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("user/createSuccess");
		mav.addObject("user", user);
		return mav;
	}
	
	@RequestMapping(value = "/home",method = RequestMethod.POST)
	public String handle1(@RequestParam("userName") String userName,
			@RequestParam("password") String password) {
		
		System.out.println("login.jsp");
		System.out.println(userName);
		System.out.println(password);
		
		return "home";
	}
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
}
